﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.BusinessRules;
using FrienDev.Data;
using FreeTextBoxControls;

public partial class Controls_Activity_MessageBoard : System.Web.UI.UserControl
{
    private int m_ActivityId;
    private int m_Type;
    private int m_PageSize;
    private int m_Count;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["activityid"]))
        {
            if (m_Type == 1)
            {
                pn_HeaderDetailPage.Visible = false;
                pn_HeaderMorePage.Visible = true;
                pn_MoreMessage.Visible = false;
                Pager.Visible = true;
                Pager.PageChanged += new EventHandler(Pager_PageChanged);
                Pager.NumericButtonTextFormatString = "<span style='font-size:14px;'>{0}</span>";
            }
            bt_Submit.Click += new EventHandler(bt_Submit_Click);
            gv_TopicMessageList.RowDataBound += new GridViewRowEventHandler(gv_TopicMessageList_RowDataBound);
            gv_TopicMessageList.RowCommand += new GridViewCommandEventHandler(gv_TopicMessageList_RowCommand);
            m_ActivityId = Convert.ToInt32(Request.QueryString["activityid"]);
            if (!IsPostBack)
            {
                //txt_MesasgeWords.Text = "<font style='Font-Size:12px;'></font>";
                Page.ClientScript.RegisterClientScriptInclude("FTB-<a href='http://www.freetextbox.com' target=_blank>FreeTextBox</a>", VirtualPathUtility.MakeRelative(Request.Path, "~/scripts/FTB-<a href='http://www.freetextbox.com' target=_blank>FreeTextBox</a>.js"));
                Page.ClientScript.RegisterClientScriptInclude("FTB-Utility", VirtualPathUtility.MakeRelative(Request.Path, "~/scripts/FTB-Utility.js"));
                Page.ClientScript.RegisterClientScriptInclude("FTB-Toolbars", VirtualPathUtility.MakeRelative(Request.Path, "~/scripts/FTB-ToolbarItems.js"));
                Page.ClientScript.RegisterClientScriptInclude("FTB-ImageGallery", VirtualPathUtility.MakeRelative(Request.Path, "~/scripts/FTB-ImageGallery.js"));
                Page.ClientScript.RegisterClientScriptInclude("FTB-Pro", VirtualPathUtility.MakeRelative(Request.Path, "~/scripts/FTB-Pro.js"));
                txt_MesasgeWords.BreakMode = BreakMode.LineBreak;
                InitGrvTopicMessageList();
            }
            hl_MoreMessage.NavigateUrl = "/Activity/ActivityMessageBoard.aspx?activityId=" + m_ActivityId;
        }
    }

    void Pager_PageChanged(object sender, EventArgs e)
    {
        Initgv_TopicMessageList();
    }

    void gv_TopicMessageList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Reply")
        {
            int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
            Panel pnReplyBox = (Panel)gv_TopicMessageList.Rows[rowIndex].Cells[0].FindControl("pn_ReplyBox");
            pnReplyBox.Visible = true;
            up_MessageList.Update();
        }
        if (e.CommandName == "CancelReply")
        {
            int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
            Panel pnReplyBox = (Panel)gv_TopicMessageList.Rows[rowIndex].Cells[0].FindControl("pn_ReplyBox");
            pnReplyBox.Visible = false;
            up_MessageList.Update();
        }
        if (e.CommandName == "DeleteTopic")
        {
            int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
            Label lbTopicId = (Label)gv_TopicMessageList.Rows[rowIndex].Cells[0].FindControl("lb_TopicId");
            ActivityRules.Instance.DeleteActivityTopicMessageAndReplys(m_ActivityId, Convert.ToInt32(lbTopicId.Text));
            InitGrvTopicMessageList();
            Initgv_TopicMessageList();
            up_MessageList.Update();
        }
        if (e.CommandName == "ReplyMessage")
        {
            int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
            FreeTextBox txtReplyWords = (FreeTextBox)gv_TopicMessageList.Rows[rowIndex].Cells[0].FindControl("txt_ReplyWords");
            Label lbNoReplyWords = (Label)gv_TopicMessageList.Rows[rowIndex].Cells[0].FindControl("lb_NoReplyWords");
            //GridView gvReMessageList = (GridView)gv_TopicMessageList.Rows[rowIndex].Cells[0].FindControl("gv_ReMessageList");
            if (txtReplyWords.Text.Trim() == "")
            {
                lbNoReplyWords.Visible = true;
            }
            else
            {
                System.Threading.Thread.Sleep(1000);
                Label lbTopicId = (Label)gv_TopicMessageList.Rows[rowIndex].Cells[0].FindControl("lb_TopicId");
                ActivityRules.Instance.ReplyMessage(UserRules.Instance.GetCurrentUserId(), txtReplyWords.Text, m_ActivityId, Convert.ToInt32(lbTopicId.Text));
                InitGrvTopicMessageList();
                Initgv_TopicMessageList();
                lb_NoWords.Visible = false;
            }
            up_MessageList.Update();
        }
    }

    void gv_TopicMessageList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbTopicId = (Label)e.Row.Cells[0].FindControl("lb_TopicId");
            Label lbSTUserId = (Label)e.Row.Cells[0].FindControl("lb_STUserId");
            User stuser = UserRules.Instance.GetUserByUserId(lbSTUserId.Text);
            HyperLink hlSTUserPage = (HyperLink)e.Row.Cells[0].FindControl("hl_STUserPage");
            hlSTUserPage.NavigateUrl = ApiHelper.Instance.GetUserHomepageLink(lbSTUserId.Text);
            HyperLink hlSTUserName = (HyperLink)e.Row.Cells[0].FindControl("hl_STUserName");
            hlSTUserName.Text = stuser.FullName;
            hlSTUserName.NavigateUrl = ApiHelper.Instance.GetUserHomepageLink(lbSTUserId.Text);
            Image imgUserImage = (Image)e.Row.Cells[0].FindControl("img_UserImage");
            imgUserImage.ImageUrl = ApiHelper.Instance.GetUserLogoUrl(lbSTUserId.Text);
            HyperLink hlShortMessage = (HyperLink)e.Row.Cells[0].FindControl("hl_ShortMessage");
            hlShortMessage.Visible = true;
            hlShortMessage.NavigateUrl = "~/Home/SendShortMessage.aspx?uids=" + lbSTUserId.Text;
            Label lbSTMessageContent = (Label)e.Row.Cells[0].FindControl("lb_STMessageContent");
            lbSTMessageContent.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
            //lbSTMessageContent.Text = HtmlHelper.HtmlEncode(lbSTMessageContent.Text);
            FreeTextBox txtReplyWords = (FreeTextBox)e.Row.Cells[0].FindControl("txt_ReplyWords");
            //txtReplyWords.Text = "<font style='Font-Size:12px;'></font>";
            //RequiredFieldValidator rfv_reply = (RequiredFieldValidator)e.Row.Cells[0].FindControl("rfv_ReplyWords");
            //rfv_reply.ValidationGroup = lbTopicId.Text;
            //LinkButton lbReplyMessage = (LinkButton)e.Row.Cells[0].FindControl("lb_ReplyMessage");
            //lbReplyMessage.ValidationGroup = lbTopicId.Text;

            GridView gvReMessageList = (GridView)e.Row.Cells[0].FindControl("gv_ReMessageList");
            gvReMessageList.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
            gvReMessageList.RowDataBound += new GridViewRowEventHandler(gvReMessageList_RowDataBound);
            gvReMessageList.DataSource = ActivityRules.Instance.GetActivityReplyMessageByParentId(m_ActivityId, Convert.ToInt32(lbTopicId.Text));
            gvReMessageList.DataBind();
            if (UserRules.Instance.GetCurrentUserId() == lbSTUserId.Text)
            {
                LinkButton lbDelete = (LinkButton)e.Row.Cells[0].FindControl("lb_Delete");
                lbDelete.Visible = true;
                lbDelete.OnClientClick = "return confirm('确定要删除这条留言及它的所有回复么？')";
            }
        }
    }

    void gvReMessageList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbReplyUserId = (Label)e.Row.Cells[0].FindControl("lb_ReplyUserId");
            User replyuser = UserRules.Instance.GetUserByUserId(lbReplyUserId.Text);
            HyperLink hlReplyUserName = (HyperLink)e.Row.Cells[0].FindControl("hl_ReplyUserName");
            Label lbReplyMessageContent = (Label)e.Row.Cells[0].FindControl("lb_ReplyMessageContent");
            //lbReplyMessageContent.Text = HtmlHelper.HtmlEncode(lbReplyMessageContent.Text);
            lbReplyMessageContent.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
            if (UserRules.Instance.GetCurrentUserId() == lbReplyUserId.Text)
            {
                hlReplyUserName.Text = "我";
                hlReplyUserName.NavigateUrl = "";
            }
            else
            {
                hlReplyUserName.Text = replyuser.FullName;
                hlReplyUserName.NavigateUrl = ApiHelper.Instance.GetUserHomepageLink(lbReplyUserId.Text);
            }

        }
    }

    void bt_Submit_Click(object sender, EventArgs e)
    {
        if (txt_MesasgeWords.Text.Trim() != "")
        {
            System.Threading.Thread.Sleep(1000);
            ActivityRules.Instance.SendMessage(UserRules.Instance.GetCurrentUserId(), txt_MesasgeWords.Text, m_ActivityId);
            Pager.CurrentPageIndex = 1;
            InitGrvTopicMessageList();
            Initgv_TopicMessageList();
            txt_MesasgeWords.Text = "";
            lb_NoWords.Visible = false;
            //txt_MesasgeWords.Text = "<font style='Font-Size:12px;'></font>";
        }
        else
        {
            lb_NoWords.Visible = true;
        }
        up_MessageList.Update();
    }

    private void InitGrvTopicMessageList()
    {
        gv_TopicMessageList.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
        if (m_Type == 0)
        {
            gv_TopicMessageList.DataSource = ActivityRules.Instance.GetActivityTopicMessageFor10(m_ActivityId);
            gv_TopicMessageList.DataBind();
        }
        else
        {
            m_Count = ActivityRules.Instance.GetActivityTopicMessageCount(m_ActivityId);
            this.Pager.RecordCount = m_Count;
            m_PageSize = this.Pager.PageSize;
        }
    }

    private void Initgv_TopicMessageList()
    {
        if (m_Type != 0)
        {
            gv_TopicMessageList.DataSource = ActivityRules.Instance.GetActivityTopicMessage(m_ActivityId, (this.Pager.CurrentPageIndex - 1) * m_PageSize, m_PageSize);
            gv_TopicMessageList.DataBind();
        }
    }

    #region Property
    public int ShowType  //0--简化留言板，1--更多留言板
    {
        set
        {
            this.m_Type = value;
        }
    }
    #endregion
}
